﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Activation;

namespace MapService
{
    [AspNetCompatibilityRequirements(RequirementsMode
          = AspNetCompatibilityRequirementsMode.Allowed)]
    public class Service1 : IService1
    {
        static string connectionString = @"Data Source=(Local)\SqlExpress;" +
              @"AttachDbFilename=|DataDirectory|GoogleMap.mdf;" +
              "Integrated Security=true;" +
              "User Instance=true";
        DuLieuDataContext dc = new DuLieuDataContext(connectionString);
        public TAIKHOAN getTK(string user_name, string pw)
        {
            try
            {
                return dc.TAIKHOANs.First(p => (p.UserName.Contains( user_name)) & (p.PassWord.Contains(pw)));
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        public bool SaveTK(TAIKHOAN sv)
        {
            try
            {
                dc.TAIKHOANs.InsertOnSubmit(sv);
                dc.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        public bool updateTK(TAIKHOAN Sv)
        {
            try
            {
                TAIKHOAN sv = dc.TAIKHOANs.Single(p => p.ID == Sv.ID);
                sv.UserName = Sv.UserName;
                sv.PassWord = Sv.PassWord;
                sv.Email = Sv.Email;
                dc.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        public bool SaveDiaDiem(DIADIEM _diadiem)
        {
            try
            {
                dc.DIADIEMs.InsertOnSubmit(_diadiem);
                dc.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        /// <summary>
        /// Cập Nhật Một Địa Điểm
        /// </summary>
        /// <param name="_diadiem">Dịa Điểm</param>
        /// <returns> True: Thành công|| False: Thất Bại</returns>
        public bool Update(DIADIEM _diadiem)
        {
            try
            {
                DIADIEM dd = dc.DIADIEMs.Single(p => p.ID == _diadiem.ID);
                dd.TenDiaDiem = _diadiem.TenDiaDiem;
                dd.ID_TaiKhoan = _diadiem.ID_TaiKhoan;
                dd.ID_DichVu = _diadiem.ID_DichVu;
                dd.TungDo = _diadiem.TungDo;
                dd.ViDo = _diadiem.ViDo;
                dd.GhiChu = _diadiem.GhiChu;
                dc.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        public bool deleteDiaDiem(int ID)
        {
            try
            {
                DIADIEM sv = dc.DIADIEMs.Single(p => p.ID == ID);
                dc.DIADIEMs.DeleteOnSubmit(sv);
                dc.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }


        public List<DIADIEM> getDSDiaDiemFromDichVu(int id)
        {
            var rs = from p in dc.DIADIEMs
                     where p.DICHVU.ID == id
                     select p;
            return rs.ToList();
        }
        public List<DIADIEM> getDSDiaDiemFromUSer(int id_user)
        {
            var rs = from p in dc.DIADIEMs
                     where p.TAIKHOAN.ID == id_user
                     select p;
            return rs.ToList();
        }
        public List<DICHVU> getDSDichVu()
        { }
      
    }
}
